package net.huashitong.Indicators.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 求x天后(前)的时间
     *
     * @param date 时间
     * @param day  天(+为x天后,-为x天前)
     * @return 计算后的时间
     * @throws ParseException
     */
    public static String addDays(String dateStr, int day) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            dateStr = dateStr.replace("-", ".");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(dateStr));
            cal.add(Calendar.DATE, day);
            return sdf.format(cal.getTime());
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 求x月后(前)的时间
     *
     * @param dateStr 时间
     * @param month   天(+为x月后,-为x月前)
     * @return 计算后的时间
     * @throws ParseException
     */
    public static String addMonths(String dateStr, int month) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(dateStr));
            cal.add(Calendar.MONTH, month);
            return sdf.format(cal.getTime());
        } catch (Exception e) {
            return null;
        }

    }


    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int betweenTwoDateNum(String date1, String date2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            int days = (int) ((sdf.parse(date2).getTime() - sdf.parse(date1).getTime()) / (1000 * 3600 * 24));
            return days;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 求当前日期
     *
     * @return
     */
    public static String getNowDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        return df.format(new Date());
    }

    public static String getDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd");//设置日期格式
        return df.format(new Date());
    }

    public static String getDateByMD() {
        SimpleDateFormat df = new SimpleDateFormat("MM月dd日");//设置日期格式
        return df.format(new Date());
    }

    public static String getDateByYMD() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");//设置日期格式
        return df.format(new Date());
    }

    public static void main(String[] args) {
        //System.out.println(addDays("2017.11.14", 2));
        System.out.println(betweenTwoDateNum("2017.11.15", "2017.11.16"));
    }

    /**
     * 年月返回上个季度
     *
     * @return
     */
    public static String getQuarterByDate(String date) {
        String year = date.substring(0, 4);
        int yearInt = Integer.parseInt(year);
        String month = date.substring(4, 6);
        Date now = null;
        if ("01".equals(month) || "02".equals(month)) {
            month = "12";
            year = yearInt - 1 + "";
        } else if ("04".equals(month) || "05".equals(month)) {
            month = "03";
        } else if ("07".equals(month) || "08".equals(month)) {
            month = "06";
        } else if ("10".equals(month) || "11".equals(month)) {
            month = "09";
        }
        return year+month;
    }

}
